package org.example.ui.log_registration;

import org.apache.commons.mail2.core.EmailException;
import org.example.business.UserBusiness;
import org.example.manage.UserManage;
import org.example.model.User;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;

public class RegistrationPanel extends JPanel {
    private final JButton cancel, registration, send;
    private final JTextField account, code;
    private final JPasswordField password, confirmPassword;
    private final JLabel accountText, passwordText, loginText, registrationText,
            codeText, confirmPasswordText;
    private final Font font;
    private final ImageIcon background;
    private String strCode = "";
    private final CardLayout cardLayout;
    private final JPanel container;
    private final JCheckBox checkBox, confirmBox;
    public RegistrationPanel(CardLayout cardLayout, JPanel container) {
        setLayout(null);
        background = new ImageIcon("data/background/微信图片_20241127230541.jpg");
        setBounds(0,0,800,600);
        this.cardLayout = cardLayout;
        this.container = container;
        checkBox = new JCheckBox();
        confirmBox = new JCheckBox();
        font = new Font("楷体",Font.BOLD,15);
        registration = new JButton("注册");
        cancel = new JButton("取消");
        loginText = new JLabel("登录");
        registrationText = new JLabel("注册");
        account = new JTextField(11);
        accountText = new JLabel("邮箱");
        passwordText = new JLabel("密码");
        password = new JPasswordField(11);
        password.setEchoChar('*');
        code = new JTextField(6);
        codeText = new JLabel("验证码");
        send = new JButton("发送");
        confirmPasswordText = new JLabel("确认密码");
        confirmPassword = new JPasswordField(11);
        confirmPassword.setEchoChar('*');
        setAll();
        setAllListener();
        addAll();
        setVisible(true);
    }

    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(background.getImage(),0,0,800,600,null);
    }
    private void setAll() {
        loginText.setBounds(460,60,50,40);
        loginText.setFont(font);
        registration.setFont(font);
        checkBox.setBounds(500,160,50,50);
        checkBox.setFont(font);
        checkBox.setContentAreaFilled(false);
        confirmBox.setFont(font);
        confirmBox.setBounds(500,220,50,50);
        confirmBox.setContentAreaFilled(false);
        registrationText.setFont(new Font("楷体",10,30));
        registrationText.setBounds(360,50,100,50);
        registration.setFont(font);
        cancel.setFont(font);
        cancel.setBounds(240,360,80,40);
        registration.setBounds(440,360,80,40);
        account.setBounds(300,100,200,50);
        account.setFont(font);
        accountText.setFont(font);
        accountText.setBounds(240,100,50,50);
        passwordText.setFont(font);
        passwordText.setBounds(240,160,50,50);
        password.setBounds(300,160,200,50);
        password.setFont(font);
        code.setBounds(300,280,100,50);
        code.setFont(font);
        codeText.setFont(font);
        codeText.setBounds(240,280,50,50);
        send.setFont(new Font("",10,15));
        send.setBounds(410,288,70,35);
        confirmPasswordText.setFont(font);
        confirmPasswordText.setBounds(240,220,100,50);
        confirmPassword.setFont(font);
        confirmPassword.setBounds(300,220,200,50);
    }
    private void setAllListener() {
        send.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                try {
                    VerificationCode verificationCode = new VerificationCode();
                    verificationCode.sendCode(account.getText());
                    strCode = verificationCode.getCode();
                } catch (EmailException ex) {
                    JOptionPane.showMessageDialog(RegistrationPanel.this, "邮箱错误");
                }
            }
        });
        password.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {

            }

            @Override
            public void focusLost(FocusEvent e) {

            }
        });
        loginText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                cardLayout.show(container,"1");
            }
        });
        registration.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                UserManage dataBase = new UserManage();
                if(UserBusiness.registration(RegistrationPanel.this, account.getText(),
                        Arrays.toString(password.getPassword()),
                        Arrays.toString(confirmPassword.getPassword()), code.getText(), strCode)){
                    JOptionPane.showMessageDialog(RegistrationPanel.this, "注册成功");
                    User user = new User();
                    user.mailbox = account.getText();
                    user.password = Arrays.toString(password.getPassword());
                    user.orderPath = account.getText() + ".json";
                    user.setAvatarPath();
                    dataBase.insertUser(user);
                    account.setText("");
                    password.setText("");
                    confirmPassword.setText("");
                    code.setText("");
                }
                dataBase.close();
            }
        });
        checkBox.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if (checkBox.isSelected()) {
                    password.setEchoChar((char)0);
                }
                else password.setEchoChar('*');
            }
        });
        confirmBox.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if (confirmBox.isSelected()) {
                    confirmPassword.setEchoChar((char)0);
                }
                else confirmPassword.setEchoChar('*');
            }
        });
        cancel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.exit(0);
            }
        });
    }
    private void addAll() {
        add(confirmPassword);
        add(confirmBox);
        add(checkBox);
        add(confirmPasswordText);
        add(code);
        add(codeText);
        add(send);
        add(account);
        add(passwordText);
        add(accountText);
        add(loginText);
        add(registration);
        add(cancel);
        add(password);
        add(registrationText);
    }
}
